package com.yyk.yykywd.mvvm.model

import com.yyk.yykywd.*
import com.yyk.yykywd.base.BaseResponse
import com.yyk.yykywd.bean.BaseBean
import com.yyk.yykywd.http.ApiService
import com.yyk.yykywd.mvvm.entity.AgentDetail1Bean
import com.yyk.yykywd.mvvm.entity.AgentDetailBean
import com.yyk.yykywd.mvvm.entity.FullContractBean
import com.yyk.yykywd.mvvm.entity.HospitalDetailBean
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers

/**
 * 文件描述：
 * Created by guo on 2019-11-19.
 */
class ReturnMoneyModel {
    fun getReturnMoneyList(type: String, page: String, num: String, getFullContract: GetFullContract) {
        val newMap = getNewMap()
        putUserId(newMap)
        putPage(newMap, page)
        putNum(newMap, num)
        putSign(newMap)
        val observable: Observable<FullContractBean> = if (type == FROM_RETURN_MONEY_AGENT) {
            ApiService.getInstance().api.postContracts(newMap)
        } else {
            ApiService.getInstance().api.postInsertHos(newMap)
        }
        observable
                .subscribeOn(Schedulers.io()) //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io()) //取消订阅
                .subscribe(object : Observer<FullContractBean> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable?) {
                    }

                    override fun onNext(t: FullContractBean) {
                        getFullContract.getInfo(t)
                    }

                    override fun onError(e: Throwable) {
                        getFullContract.onError(e.toString())
                    }

                })
    }

    fun getAgentDetail(id: String, getAgentDetail: GetAgentDetail) {
        val newMap = getNewMap()
        putId(newMap, id)
        putSign(newMap)
        ApiService.getInstance().api.postAgentDetail(newMap)
                .subscribeOn(Schedulers.io()) //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io()) //取消订阅
                .subscribe(object : Observer<AgentDetailBean> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable?) {
                    }

                    override fun onNext(t: AgentDetailBean) {
                        getAgentDetail.getInfo(t)
                    }

                    override fun onError(e: Throwable) {
                        getAgentDetail.onError(e.toString())
                    }

                })
    }

    fun getAgentDetail1(id: String, getAgentDetail: GetAgentDetail1) {
        val newMap = getNewMap()
        putId(newMap, id)
        putSign(newMap)
        ApiService.getInstance().api.postAgentDetail1(newMap)
                .subscribeOn(Schedulers.io()) //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io()) //取消订阅
                .subscribe(object : Observer<AgentDetail1Bean> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable?) {
                    }

                    override fun onNext(t: AgentDetail1Bean) {
                        getAgentDetail.getInfo(t)
                    }

                    override fun onError(e: Throwable) {
                        getAgentDetail.onError(e.toString())
                    }

                })
    }

    fun getHosDetail(id: String, getHospitalDetail: GetHospitalDetail) {
        val newMap = getNewMap()
        putId(newMap, id)
        putSign(newMap)
        ApiService.getInstance().api.getHosDetails(newMap)
                .subscribeOn(Schedulers.io()) //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io()) //取消订阅
                .subscribe(object : Observer<HospitalDetailBean> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable?) {
                    }

                    override fun onNext(t: HospitalDetailBean) {
                        getHospitalDetail.getInfo(t)
                    }

                    override fun onError(e: Throwable) {
                    }

                })
    }

    fun setC(from: String, type: String, id: String, getBase: GetBase) {
        val newMap = getNewMap()

        if (from == SET_C) {
            putKid(newMap, id)
            putUserId(newMap)
            putType(newMap, type)
        } else if (from == INSTALL_COMPLETE) {
            putKid(newMap, id)
            putUserId(newMap)
            putClientType(newMap, type)
        } else {
            putId(newMap, id)
            putUid(newMap)
            putType(newMap, type)
        }
        putSign(newMap)
        val observable: Observable<BaseBean> = if (from == SET_C) {
            ApiService.getInstance().api.postSetC(newMap)
        } else if (from == INSTALL_COMPLETE) {
            ApiService.getInstance().api.postInstallOk(newMap)
        } else {
            ApiService.getInstance().api.postThrow(newMap)
        }
        observable
                .subscribeOn(Schedulers.io()) //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io()) //取消订阅
                .subscribe(object : Observer<BaseBean> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable?) {
                    }

                    override fun onNext(t: BaseBean) {
                        getBase.getInfo(t)
                    }

                    override fun onError(e: Throwable) {
                        getBase.onError(e.toString())
                    }

                })
    }

    fun backMoney(pact_id: String, id: String, give_money: String, receipt: String, desc: String, getBase: GetBase) {
        val newMap = getNewMap()
        putKid(newMap, id)
        putPactId(newMap,pact_id)
//        putClientType(newMap, "2")
        putUserId(newMap)
        putGiveMoney(newMap, give_money)
        putGetMoneyImg(newMap, receipt)
        if (desc.isNotEmpty()) {
            putBackMoneyDesc(newMap, desc)
        }
        putSign(newMap)
        ApiService.getInstance().api.postBackMoney(newMap)
                .subscribeOn(Schedulers.io()) //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io()) //取消订阅
                .subscribe(object : Observer<BaseBean> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable?) {
                    }

                    override fun onNext(t: BaseBean) {
                        getBase.getInfo(t)
                    }

                    override fun onError(e: Throwable) {
                        getBase.onError(e.toString())
                    }

                })
    }

    interface GetHospitalDetail : BaseResponse {
        fun getInfo(hospitalDetailBean: HospitalDetailBean)
    }

    interface GetFullContract : BaseResponse {
        fun getInfo(fullContract: FullContractBean)
    }

    interface GetAgentDetail : BaseResponse {
        fun getInfo(agentDetail: AgentDetailBean)
    }

    interface GetAgentDetail1 : BaseResponse {
        fun getInfo(agentDetail: AgentDetail1Bean)
    }
}